home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / berkeleydb1.73 / Berkeley_db / mpool / mpool.c next >
Encoding:
C/C++ Source or Header  |  1994-07-27  |  11.6 KB  |  547 lines  |  [TEXT/MPS ]

  1. /*-
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)mpool.c    8.1 (Berkeley) 6/6/93";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #if defined(macintosh) && (defined(powerc) || defined(__powerc))
  39. #include "OurMalloc.h"
  40. #endif
  41.  
  42. #include <sys/param.h>
  43. #include <sys/stat.h>
  44.  
  45. #include <errno.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <unistd.h>
  50.  
  51. #ifdef macintosh
  52. #include <sys/errno.h>
  53. #endif
  54.  
  55. #include <db.h>
  56. #define    __MPOOLINTERFACE_PRIVATE
  57. #include "mpool.h"
  58.  
  59. static BKT *mpool_bkt __P((MPOOL *));
  60. static BKT *mpool_look __P((MPOOL *, pgno_t));
  61. static int  mpool_write __P((MPOOL *, BKT *));
  62. #ifdef DEBUG
  63. static void __mpoolerr __P((const char *fmt, ...));
  64. #endif
  65.  
  66. /*
  67.  * MPOOL_OPEN -- initialize a memory pool.
  68.  *
  69.  * Parameters:
  70.  *    key:        Shared buffer key.
  71.  *    fd:        File descriptor.
  72.  *    pagesize:    File page size.
  73.  *    maxcache:    Max number of cached pages.
  74.  *
  75.  * Returns:
  76.  *    MPOOL pointer, NULL on error.
  77.  */
  78. MPOOL *
  79. mpool_open(key, fd, pagesize, maxcache)
  80.     DBT *key;
  81.     int fd;
  82.     pgno_t pagesize, maxcache;
  83. {
  84.     struct stat sb;
  85.     MPOOL *mp;
  86.     int entry;
  87.  
  88.     if (fstat(fd, &sb))
  89.         return (NULL);
  90.     /* XXX
  91.      * We should only set st_size to 0 for pipes -- 4.4BSD has the fix so
  92.      * that stat(2) returns true for ISSOCK on pipes.  Until then, this is
  93.      * fairly close.
  94.      */
  95.     if (!S_ISREG(sb.st_mode)) {
  96.         errno = ESPIPE;
  97.         return (NULL);
  98.     }
  99.  
  100.     if ((mp = malloc(sizeof(MPOOL))) == NULL)
  101.         return (NULL);
  102.     mp->free.cnext = mp->free.cprev = (BKT *)&mp->free;
  103.     mp->lru.cnext = mp->lru.cprev = (BKT *)&mp->lru;
  104.     for (entry = 0; entry < HASHSIZE; ++entry)
  105.         mp->hashtable[entry].hnext = mp->hashtable[entry].hprev = 
  106.             mp->hashtable[entry].cnext = mp->hashtable[entry].cprev =
  107.             (BKT *)&mp->hashtable[entry];
  108.     mp->curcache = 0;
  109.     mp->maxcache = maxcache;
  110.     mp->pagesize = pagesize;
  111.     mp->npages = sb.st_size / pagesize;
  112.     mp->fd = fd;
  113.     mp->pgcookie = NULL;
  114.     mp->pgin = mp->pgout = NULL;
  115.  
  116. #ifdef STATISTICS
  117.     mp->cachehit = mp->cachemiss = mp->pagealloc = mp->pageflush = 
  118.         mp->pageget = mp->pagenew = mp->pageput = mp->pageread = 
  119.         mp->pagewrite = 0;
  120. #endif
  121.     return (mp);
  122. }
  123.  
  124. /*
  125.  * MPOOL_FILTER -- initialize input/output filters.
  126.  *
  127.  * Parameters:
  128.  *    pgin:        Page in conversion routine.
  129.  *    pgout:        Page out conversion routine.
  130.  *    pgcookie:    Cookie for page in/out routines.
  131.  */
  132. void
  133. mpool_filter(mp, pgin, pgout, pgcookie)
  134.     MPOOL *mp;
  135.     void (*pgin) __P((void *, pgno_t, void *));
  136.     void (*pgout) __P((void *, pgno_t, void *));
  137.     void *pgcookie;
  138. {
  139.     mp->pgin = pgin;
  140.     mp->pgout = pgout;
  141.     mp->pgcookie = pgcookie;
  142. }
  143.     
  144. /*
  145.  * MPOOL_NEW -- get a new page
  146.  *
  147.  * Parameters:
  148.  *    mp:        mpool cookie
  149.  *    pgnoadddr:    place to store new page number
  150.  * Returns:
  151.  *    RET_ERROR, RET_SUCCESS
  152.  */
  153. void *
  154. mpool_new(mp, pgnoaddr)
  155.     MPOOL *mp;
  156.     pgno_t *pgnoaddr;
  157. {
  158.     BKT *b;
  159.     BKTHDR *hp;
  160.  
  161. #ifdef STATISTICS
  162.     ++mp->pagenew;
  163. #endif
  164.     /*
  165.      * Get a BKT from the cache.  Assign a new page number, attach it to
  166.      * the hash and lru chains and return.
  167.      */
  168.     if ((b = mpool_bkt(mp)) == NULL)
  169.         return (NULL);
  170.     *pgnoaddr = b->pgno = mp->npages++;
  171.     b->flags = MPOOL_PINNED;
  172.     inshash(b, b->pgno);
  173.     inschain(b, &mp->lru);
  174.     return (b->page);
  175. }
  176.  
  177. /*
  178.  * MPOOL_GET -- get a page from the pool
  179.  *
  180.  * Parameters:
  181.  *    mp:    mpool cookie
  182.  *    pgno:    page number
  183.  *    flags:    not used
  184.  *
  185.  * Returns:
  186.  *    RET_ERROR, RET_SUCCESS
  187.  */
  188. void *
  189. mpool_get(mp, pgno, flags)
  190.     MPOOL *mp;
  191.     pgno_t pgno;
  192.     u_int flags;        /* XXX not used? */
  193. {
  194.     BKT *b;
  195.     BKTHDR *hp;
  196.     off_t off;
  197.     int nr;
  198.  
  199.     /*
  200.      * If asking for a specific page that is already in the cache, find
  201.      * it and return it.
  202.      */
  203.     if (b = mpool_look(mp, pgno)) {
  204. #ifdef STATISTICS
  205.         ++mp->pageget;
  206. #endif
  207. #ifdef DEBUG
  208.         if (b->flags & MPOOL_PINNED)
  209.             __mpoolerr("mpool_get: page %d already pinned",
  210.                 b->pgno);
  211. #endif
  212.         rmchain(b);
  213.         inschain(b, &mp->lru);
  214.         b->flags |= MPOOL_PINNED;
  215.         return (b->page);
  216.     }
  217.  
  218.     /* Not allowed to retrieve a non-existent page. */
  219.     if (pgno >= mp->npages) {
  220.         errno = EINVAL;
  221.         return (NULL);
  222.     }
  223.  
  224.     /* Get a page from the cache. */
  225.     if ((b = mpool_bkt(mp)) == NULL)
  226.         return (NULL);
  227.     b->pgno = pgno;
  228.     b->flags = MPOOL_PINNED;
  229.  
  230. #ifdef STATISTICS
  231.     ++mp->pageread;
  232. #endif
  233.     /* Read in the contents. */
  234.     off = mp->pagesize * pgno;
  235.     if (lseek(mp->fd, off, SEEK_SET) != off)
  236.         return (NULL);
  237.     if ((nr = read(mp->fd, b->page, mp->pagesize)) != mp->pagesize) {
  238.         if (nr >= 0)
  239.             errno = EFTYPE;
  240.         return (NULL);
  241.     }
  242.     if (mp->pgin)
  243.         (mp->pgin)(mp->pgcookie, b->pgno, b->page);
  244.  
  245.     inshash(b, b->pgno);
  246.     inschain(b, &mp->lru);
  247. #ifdef STATISTICS
  248.     ++mp->pageget;
  249. #endif
  250.     return (b->page);
  251. }
  252.  
  253. /*
  254.  * MPOOL_PUT -- return a page to the pool
  255.  *
  256.  * Parameters:
  257.  *    mp:    mpool cookie
  258.  *    page:    page pointer
  259.  *    pgno:    page number
  260.  *
  261.  * Returns:
  262.  *    RET_ERROR, RET_SUCCESS
  263.  */
  264. int
  265. mpool_put(mp, page, flags)
  266.     MPOOL *mp;
  267.     void *page;
  268.     u_int flags;
  269. {
  270.     BKT *baddr;
  271. #ifdef DEBUG
  272.     BKT *b;
  273. #endif
  274.  
  275. #ifdef STATISTICS
  276.     ++mp->pageput;
  277. #endif
  278.     baddr = (BKT *)((char *)page - sizeof(BKT));
  279. #ifdef DEBUG
  280.     if (!(baddr->flags & MPOOL_PINNED))
  281.         __mpoolerr("mpool_put: page %d not pinned", b->pgno);
  282.     for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
  283.         if (b == (BKT *)&mp->lru)
  284.             __mpoolerr("mpool_put: %0x: bad address", baddr);
  285.         if (b == baddr)
  286.             break;
  287.     }
  288. #endif
  289.     baddr->flags &= ~MPOOL_PINNED;
  290.     baddr->flags |= flags & MPOOL_DIRTY;
  291.     return (RET_SUCCESS);
  292. }
  293.  
  294. /*
  295.  * MPOOL_CLOSE -- close the buffer pool
  296.  *
  297.  * Parameters:
  298.  *    mp:    mpool cookie
  299.  *
  300.  * Returns:
  301.  *    RET_ERROR, RET_SUCCESS
  302.  */
  303. int
  304. mpool_close(mp)
  305.     MPOOL *mp;
  306. {
  307.     BKT *b, *next;
  308.  
  309.     /* Free up any space allocated to the lru pages. */
  310.     for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = next) {
  311.         next = b->cprev;
  312.         free(b);
  313.     }
  314.     free(mp);
  315.     return (RET_SUCCESS);
  316. }
  317.  
  318. /*
  319.  * MPOOL_SYNC -- sync the file to disk.
  320.  *
  321.  * Parameters:
  322.  *    mp:    mpool cookie
  323.  *
  324.  * Returns:
  325.  *    RET_ERROR, RET_SUCCESS
  326.  */
  327. int
  328. mpool_sync(mp)
  329.     MPOOL *mp;
  330. {
  331.     BKT *b;
  332.  
  333.     for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
  334.         if (b->flags & MPOOL_DIRTY && mpool_write(mp, b) == RET_ERROR)
  335.             return (RET_ERROR);
  336. #ifdef macintosh
  337.     return RET_SUCCESS;
  338. #else
  339.     return (fsync(mp->fd) ? RET_ERROR : RET_SUCCESS);
  340. #endif
  341. }
  342.  
  343. /*
  344.  * MPOOL_BKT -- get/create a BKT from the cache
  345.  *
  346.  * Parameters:
  347.  *    mp:    mpool cookie
  348.  *
  349.  * Returns:
  350.  *    NULL on failure and a pointer to the BKT on success    
  351.  */
  352. static BKT *
  353. mpool_bkt(mp)
  354.     MPOOL *mp;
  355. {
  356.     BKT *b;
  357.  
  358.     if (mp->curcache < mp->maxcache)
  359.         goto new;
  360.  
  361.     /*
  362.      * If the cache is maxxed out, search the lru list for a buffer we
  363.      * can flush.  If we find one, write it if necessary and take it off
  364.      * any lists.  If we don't find anything we grow the cache anyway.
  365.      * The cache never shrinks.
  366.      */
  367.     for (b = mp->lru.cprev; b != (BKT *)&mp->lru; b = b->cprev)
  368.         if (!(b->flags & MPOOL_PINNED)) {
  369.             if (b->flags & MPOOL_DIRTY &&
  370.                 mpool_write(mp, b) == RET_ERROR)
  371.                 return (NULL);
  372.             rmhash(b);
  373.             rmchain(b);
  374. #ifdef STATISTICS
  375.             ++mp->pageflush;
  376. #endif
  377. #ifdef DEBUG
  378.             {
  379.                 void *spage;
  380.                 spage = b->page;
  381.                 memset(b, 0xff, sizeof(BKT) + mp->pagesize);
  382.                 b->page = spage;
  383.             }
  384. #endif
  385.             return (b);
  386.         }
  387.  
  388. new:    if ((b = malloc(sizeof(BKT) + mp->pagesize)) == NULL)
  389.         return (NULL);
  390. #ifdef STATISTICS
  391.     ++mp->pagealloc;
  392. #endif
  393. #ifdef DEBUG
  394.     memset(b, 0xff, sizeof(BKT) + mp->pagesize);
  395. #endif
  396.     b->page = (char *)b + sizeof(BKT);
  397.     ++mp->curcache;
  398.     return (b);
  399. }
  400.  
  401. /*
  402.  * MPOOL_WRITE -- sync a page to disk
  403.  *
  404.  * Parameters:
  405.  *    mp:    mpool cookie
  406.  *
  407.  * Returns:
  408.  *    RET_ERROR, RET_SUCCESS
  409.  */
  410. static int
  411. mpool_write(mp, b)
  412.     MPOOL *mp;
  413.     BKT *b;
  414. {
  415.     off_t off;
  416.  
  417.     if (mp->pgout)
  418.         (mp->pgout)(mp->pgcookie, b->pgno, b->page);
  419.  
  420. #ifdef STATISTICS
  421.     ++mp->pagewrite;
  422. #endif
  423.     off = mp->pagesize * b->pgno;
  424.     if (lseek(mp->fd, off, SEEK_SET) != off)
  425.         return (RET_ERROR);
  426.     if (write(mp->fd, b->page, mp->pagesize) != mp->pagesize)
  427.         return (RET_ERROR);
  428.     b->flags &= ~MPOOL_DIRTY;
  429.     return (RET_SUCCESS);
  430. }
  431.  
  432. /*
  433.  * MPOOL_LOOK -- lookup a page
  434.  *
  435.  * Parameters:
  436.  *    mp:    mpool cookie
  437.  *    pgno:    page number
  438.  *
  439.  * Returns:
  440.  *    NULL on failure and a pointer to the BKT on success
  441.  */
  442. static BKT *
  443. mpool_look(mp, pgno)
  444.     MPOOL *mp;
  445.     pgno_t pgno;
  446. {
  447.     register BKT *b;
  448.     register BKTHDR *tb;
  449.  
  450.     /* XXX
  451.      * If find the buffer, put it first on the hash chain so can
  452.      * find it again quickly.
  453.      */
  454.     tb = &mp->hashtable[HASHKEY(pgno)];
  455.     for (b = tb->hnext; b != (BKT *)tb; b = b->hnext)
  456.         if (b->pgno == pgno) {
  457. #ifdef STATISTICS
  458.             ++mp->cachehit;
  459. #endif
  460.             return (b);
  461.         }
  462. #ifdef STATISTICS
  463.     ++mp->cachemiss;
  464. #endif
  465.     return (NULL);
  466. }
  467.  
  468. #ifdef STATISTICS
  469. /*
  470.  * MPOOL_STAT -- cache statistics
  471.  *
  472.  * Parameters:
  473.  *    mp:    mpool cookie
  474.  */
  475. void
  476. mpool_stat(mp)
  477.     MPOOL *mp;
  478. {
  479.     BKT *b;
  480.     int cnt;
  481.     char *sep;
  482.  
  483.     (void)fprintf(stderr, "%lu pages in the file\n", mp->npages);
  484.     (void)fprintf(stderr,
  485.         "page size %lu, cacheing %lu pages of %lu page max cache\n",
  486.         mp->pagesize, mp->curcache, mp->maxcache);
  487.     (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n",
  488.         mp->pageput, mp->pageget, mp->pagenew);
  489.     (void)fprintf(stderr, "%lu page allocs, %lu page flushes\n",
  490.         mp->pagealloc, mp->pageflush);
  491.     if (mp->cachehit + mp->cachemiss)
  492.         (void)fprintf(stderr,
  493.             "%.0f%% cache hit rate (%lu hits, %lu misses)\n", 
  494.             ((double)mp->cachehit / (mp->cachehit + mp->cachemiss))
  495.             * 100, mp->cachehit, mp->cachemiss);
  496.     (void)fprintf(stderr, "%lu page reads, %lu page writes\n",
  497.         mp->pageread, mp->pagewrite);
  498.  
  499.     sep = "";
  500.     cnt = 0;
  501.     for (b = mp->lru.cnext; b != (BKT *)&mp->lru; b = b->cnext) {
  502.         (void)fprintf(stderr, "%s%d", sep, b->pgno);
  503.         if (b->flags & MPOOL_DIRTY)
  504.             (void)fprintf(stderr, "d");
  505.         if (b->flags & MPOOL_PINNED)
  506.             (void)fprintf(stderr, "P");
  507.         if (++cnt == 10) {
  508.             sep = "\n";
  509.             cnt = 0;
  510.         } else
  511.             sep = ", ";
  512.             
  513.     }
  514.     (void)fprintf(stderr, "\n");
  515. }
  516. #endif
  517.  
  518. #ifdef DEBUG
  519. #if __STDC__
  520. #include <stdarg.h>
  521. #else
  522. #include <varargs.h>
  523. #endif
  524.  
  525. static void
  526. #if __STDC__
  527. __mpoolerr(const char *fmt, ...)
  528. #else
  529. __mpoolerr(fmt, va_alist)
  530.     char *fmt;
  531.     va_dcl
  532. #endif
  533. {
  534.     va_list ap;
  535. #if __STDC__
  536.     va_start(ap, fmt);
  537. #else
  538.     va_start(ap);
  539. #endif
  540.     (void)vfprintf(stderr, fmt, ap);
  541.     va_end(ap);
  542.     (void)fprintf(stderr, "\n");
  543.     abort();
  544.     /* NOTREACHED */
  545. }
  546. #endif
  547.